Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share default TimelineCredParameters #1376

Merged
merged 1 commit into from Sep 12, 2019
Merged

Share default TimelineCredParameters #1376

merged 1 commit into from Sep 12, 2019

Conversation

teamdandelion
Copy link
Contributor

@teamdandelion teamdandelion commented Sep 11, 2019

At present, every place in the codebase that needs
TimelineCredParameters constructs them ad-hoc, meaning we don't have any
shared defaults across different consumers.

This commit adds a new type, PartialTimelineCredParameters, which
is basically TimelineCredParameters with every field marked optional.
Callers can then choose to override any fields where they want
non-default values. A new internal partialParams function promotes
these partial parameters to full parameters.

All the public interfaces for using params (namely,
TimelineCred.compute and TimelineCred.reanalyze) now accept optional
partial params. If the params are not specified, default values are
used; if partial params are provided, all the explicitly provided values
are used, and unspecified values are initialized to default values.

Test plan: A simple unit test was added to ensure that weights overrides
work as intended. git grep "intervalDecay: " reveals that there are no
other explicit parameter constructions in the codebase. All existing
unit tests pass.

@@ -56,3 +57,10 @@ export function paramsFromJSON(
weights: weightsFromJSON(p.weights),
};
}

export function defaultParams(weights?: Weights): TimelineCredParameters {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I see your point about only weights being used for overrides. I feel like this approach (selective overrides, and a default parameters function) is somewhat unconventional. Most commonly I see optional arguments to a constructor or factory, which are then merged with the default values.

In some cases the defaults are also exposed, for example in the yaml package, because you may want to reuse your options object for multiple function calls. Though in this case, the TimelineCred class already acts as the way to store and reuse your options.

The bottom line is, I wouldn't make it the responsibility of the caller to know about defaultParams when constructing. Instead make it the constructor / factory's concern to fill in any parameters not provided. And not to be selective based on what's already used in the current code-base. As this convention will make exposing features in a library-style easier later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I refactored the usage so that all the API endpoints that accept parameters now accept an optional "partial parameters" object. Basically you can provide partial inputs like {weights: myCustomWeights, alpha: myCustomAlpha} and any missing fields get filled in automatically. PTAL.

At present, every place in the codebase that needs
TimelineCredParameters constructs them ad-hoc, meaning we don't have any
shared defaults across different consumers.

This commit adds a new type, `PartialTimelineCredParameters`, which
is basically `TimelineCredParameters` with every field marked optional.
Callers can then choose to override any fields where they want
non-default values. A new internal `partialParams` function promotes
these partial parameters to full parameters.

All the public interfaces for using params (namely,
`TimelineCred.compute` and `TimelineCred.reanalyze`) now accept optional
partial params. If the params are not specified, default values are
used; if partial params are provided, all the explicitly provided values
are used, and unspecified values are initialized to default values.

Test plan: A simple unit test was added to ensure that weights overrides
work as intended. `git grep "intervalDecay: "` reveals that there are no
other explicit parameter constructions in the codebase. All existing
unit tests pass.
@teamdandelion teamdandelion merged commit 0a0010f into master Sep 12, 2019
@teamdandelion teamdandelion deleted the default-params branch September 12, 2019 13:21
@wchargin
Copy link
Member

This commit adds a new type, PartialTimelineCredParameters, which
is basically TimelineCredParameters with every field marked optional.

No need; use built-in $Shape<…>: see #1379.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants